package com.xiaoyu.lanling.base.push.xiaomi;

import android.app.Activity;
import android.content.Context;

import com.xiaomi.mipush.sdk.ErrorCode;
import com.xiaomi.mipush.sdk.MiPushClient;
import com.xiaomi.mipush.sdk.MiPushCommandMessage;
import com.xiaomi.mipush.sdk.MiPushMessage;
import com.xiaoyu.base.AppConfig;
import com.xiaoyu.base.push.BasePushAgent;
import com.xiaoyu.base.push.PushConstants;

import java.util.List;

import in.srain.cube.util.CLog;
import in.srain.cube.util.DeviceInfoUtil;

public class MiPushAgent extends BasePushAgent {

    private static MiPushAgent sInstance;
    private String mRegId;

    public MiPushAgent() {
        super(PushConstants.PUSH_TYPE_XIAOMI);
    }

    public static synchronized MiPushAgent getInstance() {
        if (sInstance == null) {
            sInstance = new MiPushAgent();
        }
        return sInstance;
    }

    @Override
    public void onResume(Activity activity) {
        if (DeviceInfoUtil.isMiUi()) {
            MiPushClient.clearNotification(activity);
        }
    }

    @Override
    protected void tryToStartPushService(Context context) {
        super.tryToStartPushService(context);
        MiPushClient.registerPush(context, AppConfig.miAppId(), AppConfig.miAppKey());
    }

    public void onReceivePassThroughMessage(Context context, MiPushMessage message) {
        CLog.i(TAG, "onReceivePassThroughMessage message: %s", message);
        onPassThroughMessage(message.getContent());
    }

    public void onNotificationMessageClicked(Context context, MiPushMessage message) {
        CLog.i(TAG, "onNotificationMessageClicked message: %s", message);
        onNotificationClickData(message.getContent());
    }

    public void onNotificationMessageArrived(Context context, MiPushMessage message) {
        CLog.i(TAG, "onNotificationMessageArrived message: %s", message);
    }

    public void onCommandResult(Context context, MiPushCommandMessage message) {
        CLog.i(TAG, "onCommandResult  message: %s", message);
        String command = message.getCommand();
        List<String> arguments = message.getCommandArguments();
        String cmdArg1 = ((arguments != null && arguments.size() > 0) ? arguments.get(0) : null);
        if (MiPushClient.COMMAND_REGISTER.equals(command)) {
            if (message.getResultCode() == ErrorCode.SUCCESS) {
                mRegId = cmdArg1;
            } else {
                mRegId = null;
            }
        }
    }

    public void onReceiveRegisterResult(Context context, MiPushCommandMessage message) {
        CLog.i(TAG, "onReceiveRegisterResult message: %s", message);
        String command = message.getCommand();
        List<String> arguments = message.getCommandArguments();
        String cmdArg1 = ((arguments != null && arguments.size() > 0) ? arguments.get(0) : null);
        if (MiPushClient.COMMAND_REGISTER.equals(command)) {
            if (message.getResultCode() == ErrorCode.SUCCESS) {
                mRegId = cmdArg1;
                onRegisterSuccess(mRegId);
            } else {
                mRegId = null;
                retryOnFail(String.valueOf(message.getResultCode()));
            }
        }
    }
}
